package com.baidu.baidumap;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapViewLayoutParams;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;

/**
 * Created by killer on 2016/4/18.
 */
public class MarkerOptionsDemo extends BaseActivity {
    private View pop;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        draw();
        initPop();
    }

    private void initPop() {
        //加载Pop，添加到mapview 设置为隐藏
        pop = View.inflate(getApplicationContext(), R.layout.pop, null);

        ViewGroup.LayoutParams params = new MapViewLayoutParams.Builder()
                .layoutMode(MapViewLayoutParams.ELayoutMode.mapMode)// 按照经纬度设置位置
                .position(hmPos)// 不能传null 设置为mapMode时 必须设置position
                .width(MapViewLayoutParams.WRAP_CONTENT)
                .height(MapViewLayoutParams.WRAP_CONTENT)
                .build();
        mapview.addView(pop, params);
        pop.setVisibility(View.INVISIBLE);
        title = (TextView) pop.findViewById(R.id.title);
    }

    private void draw() {
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.eat_icon);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(hmPos)//设置位置
                .icon(bitmapDescriptor)//设置图标
                .draggable(true)//设置是否可以拖拽
                .title("killer");//设置标题
        baiduMap.addOverlay(markerOptions);

        ArrayList<BitmapDescriptor> bitMaps = new ArrayList<>();
        bitMaps.add(bitmapDescriptor);
        bitMaps.add(BitmapDescriptorFactory.fromResource(R.drawable.icon_geo));
        markerOptions = new MarkerOptions().title("向北")
                .position(new LatLng(latitude + 0.001, longitude))
                .icons(bitMaps)
                .period(10);
        baiduMap.addOverlay(markerOptions);
        markerOptions = new MarkerOptions().title("向东")
                .draggable(true)
                .position(new LatLng(latitude, longitude + 0.001))
                .icon(bitmapDescriptor);
        baiduMap.addOverlay(markerOptions);
        markerOptions = new MarkerOptions().title("向西南")
                .draggable(false)
                .position(new LatLng(latitude - 0.001, longitude - 0.001))
                .icon(bitmapDescriptor);
        baiduMap.addOverlay(markerOptions);

        baiduMap.setOnMarkerClickListener(new MyListener());
    }

    class MyListener implements BaiduMap.OnMarkerClickListener {

        @Override
        public boolean onMarkerClick(Marker marker) {
            //当点击时 更新pop的位置 设置为显示
            ViewGroup.LayoutParams params = new MapViewLayoutParams.Builder()
                    .layoutMode(MapViewLayoutParams.ELayoutMode.mapMode)//按照经纬度设置位置
                    .position(marker.getPosition())//不能传null
                    .width(MapViewLayoutParams.WRAP_CONTENT)
                    .height(MapViewLayoutParams.WRAP_CONTENT)
                    .yOffset(-5)//距离position的像素，向下是正值
                    .build();
            mapview.updateViewLayout(pop, params);
            pop.setVisibility(View.VISIBLE);

            title.setText(marker.getTitle());
            return true;
        }
    }
}
